package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsDeliverbillentry;
import com.ruoyi.basebill.mapper.OmsDeliverbillMapper;
import com.ruoyi.basebill.domain.OmsDeliverbill;
import com.ruoyi.basebill.service.IOmsDeliverbillService;

/**
 * OMS发货确认Service业务层处理
 * 
 * @author ptt
 * @date 2023-02-10
 */
@Service
public class OmsDeliverbillServiceImpl implements IOmsDeliverbillService 
{
    @Autowired
    private OmsDeliverbillMapper omsDeliverbillMapper;

    @Autowired
    private OmsDeliverbillServiceImpl deliverbillService;

    /**
     * 查询OMS发货确认
     * 
     * @param id OMS发货确认主键
     * @return OMS发货确认
     */
    @Override
    public OmsDeliverbill selectOmsDeliverbillById(Long id)
    {
        return omsDeliverbillMapper.selectOmsDeliverbillById(id);
    }

    /**
     * 查询OMS发货确认列表
     * 
     * @param omsDeliverbill OMS发货确认
     * @return OMS发货确认
     */
    @Override
    public List<OmsDeliverbill> selectOmsDeliverbillList(OmsDeliverbill omsDeliverbill)
    {
        return omsDeliverbillMapper.selectOmsDeliverbillList(omsDeliverbill);
    }

    @Override
    public List<OmsDeliverbill> selectOmsDeliverbillDetailList(OmsDeliverbill omsDeliverbill) {
        return omsDeliverbillMapper.selectOmsDeliverbillDetailList(omsDeliverbill);
    }


    /**
     * 新增OMS发货确认
     * 
     * @param omsDeliverbill OMS发货确认
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsDeliverbill(OmsDeliverbill omsDeliverbill)
    {
        int rows = omsDeliverbillMapper.insertOmsDeliverbill(omsDeliverbill);
        deliverbillService.insertOmsDeliverbillentry(omsDeliverbill);
        return rows;
    }

    /**
     * 修改OMS发货确认
     * 
     * @param omsDeliverbill OMS发货确认
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsDeliverbill(OmsDeliverbill omsDeliverbill)
    {
        //不存在分录时，不对分录进行操作
        if (omsDeliverbill.getOmsDeliverbillentryList() != null && omsDeliverbill.getOmsDeliverbillentryList().size() != 0){
            omsDeliverbillMapper.deleteOmsDeliverbillentryByParentId(omsDeliverbill.getId());
            deliverbillService.insertOmsDeliverbillentry(omsDeliverbill);
        }
        return omsDeliverbillMapper.updateOmsDeliverbill(omsDeliverbill);
    }

    /**
     * 批量删除OMS发货确认
     * 
     * @param ids 需要删除的OMS发货确认主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsDeliverbillByIds(Long[] ids)
    {
        omsDeliverbillMapper.deleteOmsDeliverbillentryByParentIds(ids);
        return omsDeliverbillMapper.deleteOmsDeliverbillByIds(ids);
    }

    /**
     * 删除OMS发货确认信息
     * 
     * @param id OMS发货确认主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsDeliverbillById(Long id)
    {
        omsDeliverbillMapper.deleteOmsDeliverbillentryByParentId(id);
        return omsDeliverbillMapper.deleteOmsDeliverbillById(id);
    }

    /**
     * 新增OMS发货确认分录信息
     * 
     * @param omsDeliverbill OMS发货确认对象
     */
    public void insertOmsDeliverbillentry(OmsDeliverbill omsDeliverbill)
    {
        List<OmsDeliverbillentry> omsDeliverbillentryList = omsDeliverbill.getOmsDeliverbillentryList();
        Long id = omsDeliverbill.getId();
        if (StringUtils.isNotNull(omsDeliverbillentryList))
        {
            List<OmsDeliverbillentry> list = new ArrayList<OmsDeliverbillentry>();
            for (OmsDeliverbillentry omsDeliverbillentry : omsDeliverbillentryList)
            {
                omsDeliverbillentry.setParentid(id);
                list.add(omsDeliverbillentry);
            }
            if (list.size() > 0)
            {
                omsDeliverbillMapper.batchOmsDeliverbillentry(list);
            }
        }
    }
}
